/*
 * Dijjer - A Peer to Peer HTTP Cache
 * Copyright (C) 2004,2005 Change.Tv, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package dijjer.io.store;

import java.io.File;
import java.io.IOException;
import dijjer.util.VeryLongInteger;
import dijjer.util.VeryLongIntegerKey;

public class HashStore extends Store {

	public static final String VERSION = "$Id: HashStore.java 2 2008-03-21 14:33:03Z ian.clarke $";

	private static final int HASH_BLOCK_SIZE = 40;
	private static HashStore _hst;

	public HashStore(File store, long maxHashes) throws Exception {
		super(store, maxHashes);
		readStore();
	}

	protected synchronized void readStore() throws IOException {
		getBlockStore().seek(0);
		int recordNum = 0;
		while (getBlockStore().getFilePointer() < getBlockStore().length()) {
			VeryLongInteger key = new VeryLongInteger(getBlockStore().readLong(), getBlockStore().readLong());
			VeryLongInteger hash =  new VeryLongInteger(getBlockStore().readLong(), getBlockStore().readLong());
			long accessTime = getBlockStore().readLong();
			addBlock(new HashBlock(recordNum++, key,accessTime, hash));
		}
	}

	public void deleteBlock(Block block, boolean wipe) throws IOException {
		HashBlock del = (HashBlock) block;
		HashBlock last = (HashBlock) getRecordNumberList().get(getRecordNumberList().size() - 1);
		getKeyMap().remove(del.getKey());
		getAccessTimeList().remove(del);

		getRecordNumberList().set(last.getRecordNumber(), null);
		getRecordNumberList().set(del.getRecordNumber(), last);
		last.setRecordNumber(del.getRecordNumber());
		writeBlock(last);
		getBlockStore().setLength(getBlockStore().length() - HASH_BLOCK_SIZE);
	}

	public synchronized VeryLongInteger getHash(VeryLongIntegerKey key) throws IOException {
		HashBlock hb = (HashBlock) getKeyMap().get(key);
		if (hb == null) {
			return null;
		}
		hb.setLastAccessTime(System.currentTimeMillis());
		writeBlock(hb);
		updateLastAccess(hb);
		writeBlock(hb);
		return hb.getHash();
	}

	public synchronized void put(VeryLongIntegerKey key, VeryLongInteger hash) throws IOException {
		if (getAccessTimeList().size() < getMaxBlocks()) {
			addBlock(new HashBlock(getAccessTimeList().size(), key, System.currentTimeMillis(), hash));
		} else {
			replaceBlock((HashBlock) getAccessTimeList().getFirst(), key, hash);
		}
	}

	public int getCurrentSize() {
		return getAccessTimeList().size();
	}
	
	private void addBlock(HashBlock hb) throws IOException {
		// Add to lastAccessTimeList
		updateLastAccess(hb);
		// Add to recordNo HashMap
		getRecordNumberList().add(hb.getRecordNumber(), hb);
		// Add to key HashMap
		getKeyMap().put(hb.getKey(), hb);
		writeBlock(hb);
	}

	private void replaceBlock(HashBlock old, VeryLongInteger key, VeryLongInteger hash) throws IOException {
		getKeyMap().remove(old.getKey());
		old.setKey(key);
		old.setHash(hash);
		getKeyMap().put(key, old);
		old.setLastAccessTime(System.currentTimeMillis());
		writeBlock(old);
		updateLastAccess(old);
		writeBlock(old);
	}

	private void writeBlock(HashBlock hb) throws IOException {
		getBlockStore().seek(hb.getRecordNumber() * HASH_BLOCK_SIZE);
		getBlockStore().writeLong(hb.getKey().getHigh());
		getBlockStore().writeLong(hb.getKey().getLow());
		getBlockStore().writeLong(hb.getHash().getHigh());
		getBlockStore().writeLong(hb.getHash().getLow());
		getBlockStore().writeLong(hb.getLastAccessTime());
	}

	public static void init(File store, int hashes) throws Exception
	{
		_hst = new HashStore(store, hashes);
	}

	public static HashStore getHashStore()
	{
		return _hst;
	}
}

class HashBlock extends Block {

	private VeryLongInteger _hash;

	public HashBlock(int recordNo, VeryLongInteger key, long lastAccessTime, VeryLongInteger hash) throws IOException {
		super(recordNo, key, lastAccessTime);
		_hash = hash;
	}

	public VeryLongInteger getHash() {
		return _hash;
	}

	public void setHash(VeryLongInteger hash) {
		_hash = hash;
	}
}

